import 'dart:developer';

import 'package:demo_flutter/pages/view/testPage/demo_view.dart';
import 'package:demo_flutter/pages/view/testPage/sqlite_database_util.dart';
import 'package:flutter/material.dart';
import 'package:sqlite3/sqlite3.dart';

// 测试页面
class DemoPage extends StatefulWidget {
  const DemoPage({super.key});

  @override
  State<DemoPage> createState() => _DemoPageState();
}

class _DemoPageState extends State<DemoPage> {
  _DemoPageState() {
    log("初始化", time: DateTime.now(), name: "info");
  }

  @override
  Widget build(BuildContext context) {
    String inputString = '';
    // 输入框
    Widget inputView = TextField(
      onChanged: (value) => {inputString = value},
      decoration: const InputDecoration(hintText: "输入测试的值"),
    );

    String response;
    int databaseIndex = 1;
    Widget getData = TextButton(
        onPressed: () => {
              log("获取数据"),
              print('Using sqlite3 ${sqlite3.version}'),
              response = sqliteUtil(databaseIndex),
              databaseIndex += 1,
              showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AlertDialog(
                      title: Text("弹窗"),
                      content: Text(response),
                      actions: <Widget>[
                        TextButton(
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                            child: Text('确认'))
                      ],
                    );
                  })
            },
        child: const Text("获取数据"));
    Widget insertData =
        TextButton(onPressed: () => {log("插入数据")}, child: const Text("插入数据"));
    // 页面跳转
    Widget jumpView = TextButton(
        onPressed: () => {
              Navigator.push(context, MaterialPageRoute(
                builder: (context) {
                  return DemoView(
                    demoInput: inputString,
                  );
                },
              ))
            },
        child: const Text("跳转到其他页面"));

    return Scaffold(
      body: ListView(
        scrollDirection: Axis.vertical,
        children: [
          // 输入框
          inputView,
          // 页面挑战
          const Text("页面跳转测试"),
          jumpView,
          const Text("数据库测试"),
          getData, insertData,
        ],
      ),
    );
  }
}
